package mybitset;

import java.util.Arrays;

public class MyBitSet {
    public byte[] elem;
    public int usedSize;
    public MyBitSet(){
        elem = new byte[1];
    }
    public MyBitSet(int n){
        elem = new byte[n/8+1];
    }
    public void set(int val){
        if(val < 0){
            throw new IndexOutOfBoundsException();
        }
        int elemIndex = val / 8;
        int bitIndex = val % 8;
        if(elemIndex > elem.length-1){
            elem = Arrays.copyOf(elem , elemIndex+1);
        }
        elem[elemIndex] |= (1 << bitIndex);
        usedSize++;
    }
    public boolean get(int val){
        int elemIndex = val / 8;
        int bitIndex = val % 8;
        if((elem[elemIndex] & (1 << bitIndex)) == 0){
            return false;
        }
        return true;
    }
    public void reSet(int val){
        int elemIndex = val / 8;
        int bitIndex = val % 8;
        elem[elemIndex] &= ~(1 << bitIndex);
        usedSize--;
    }
    public int getUsedSize(){
        return usedSize;
    }
}
